//
//  LLNetManager.swift
//  新印象
//
//  Created by wyl on 2016/12/21.
//  Copyright © 2016年 魏延龙. All rights reserved.
//

import UIKit
import Alamofire

class LLNetManager: NSObject {
    class func netGet(url : String, parameters : Dictionary<String, AnyObject>, completionHandlerSuccess:@escaping (_ response : Dictionary<String, AnyObject>) ->(),completionHandlerFailure:@escaping (_ error : Error) -> ()) {
        let header = ["apikey":"89cf9afb83dc307b66e932a30c870fb7"]
        Alamofire.request(url, method: HTTPMethod.get, parameters: parameters, encoding: URLEncoding.default, headers: header).responseJSON { (json) in
            switch json.result{
            case .success(let value):
                completionHandlerSuccess(value as! Dictionary<String, AnyObject>)
            case .failure(let Error):
                completionHandlerFailure(Error)
            }
        }
    }
    class func downLoad(string : String, fileName : String ,completionHandlerSuccess: @escaping (_ progress : Double) -> ()) {
        
        let destination: DownloadRequest.DownloadFileDestination = { _,_ in
            let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
            let fileURL = documentsURL.appendingPathComponent("file1/\(fileName).mp3")
            //两个参数表示如果有同名文件则会覆盖，如果路径中文件夹不存在则会自动创建
            return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
        }
        
        Alamofire.download(URLRequest(url: URL(string: string)!), to: destination).downloadProgress { (progress) in
            completionHandlerSuccess(progress.fractionCompleted)
        }
    
    }
        
    
}

